﻿using ConfExpert.Forms.Base;

using DevExpress.Images;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Kargo
{
    public partial class FaturaGir : XForm
    {
        public FaturaGir()
        {
            InitializeComponent();
        }

        private void FaturaGir_Load(object sender, EventArgs e)
        {

            Guncelle();

            gvMicro.RowCellClick += gvMicro_RowCellClick;
            gvProforma.RowCellClick += gvProforma_RowCellClick;

            this.btnUpdate.ItemClick += btnUpdate_ItemClick;
            this.gvProforma.DoubleClick += this.gvProforma_DoubleClick;
            gvMicro.DoubleClick += gvMicro_DoubleClick;

            GridManager gm = GridManager.GetManager(gvProforma);
            //Image close =  DevExpress.Images.ImageResourceCache.Default.GetImage("images/actions/add_16x16.png");
            System.Drawing.Image close = ImageResourceCache.Default.GetImage("images/actions/close_16x16.png");
            gm.GridMenu.AddMenu("Seçili satırları iptal Et", ProformaCancel, close);
            gm.GridMenu.AddMenu("Seçili satırları kapat", ProformaClose);

            GridManager gm2 = GridManager.GetManager(gvMicro);
            gm2.GridMenu.AddMenu("Seçili satırları iptal Et", MicroCancel, close);
            gm2.GridMenu.AddMenu("Seçili satırları kapat", MicroClose);
        }

        private void btnUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Guncelle();
        }

        private void Guncelle()
        {
            bool showAll = false;
            if (chkShowAll.Checked)
                showAll = true;

            DataSet dsFaturaGir = ExecuteDataSet("GetKargoFaturaGir", WhoAmI, showAll);
            DataTable dtMicro = dsFaturaGir.Tables[0];
            DataTable dtProforma = dsFaturaGir.Tables[1];

            gvMicro.Init(dtMicro);
            gvProforma.Init(dtProforma);

            gvMicro.Columns["KargoF"].ColumnEdit = HyperLink();
            gvMicro.Columns["MalzemeF"].ColumnEdit = HyperLink();
            gvProforma.Columns["KargoF"].ColumnEdit = HyperLink();
            gvProforma.Columns["MalzemeF"].ColumnEdit = HyperLink();

            gvMicro.MakeColumnInvisible("KargoId", "stGenelId");
            gvProforma.MakeColumnInvisible("KargoId", "stGenelId");

            gvMicro.MakeReadOnly();
            gvProforma.MakeReadOnly();

            gvMicro.BestFitColumns();
            gvProforma.BestFitColumns();
        }

        private void ProformaClose(object sender, EventArgs e)
        {
            Change_stGenelId(gvProforma, 2, "Kapatma işlemi tamamlandı.");
        }

        private void ProformaCancel(object sender, EventArgs e)
        {
            Change_stGenelId(gvProforma, 3, "İptal işlemi tamamlandı.");
        }

        private void MicroClose(object sender, EventArgs e)
        {
            Change_stGenelId(gvMicro, 2, "Kapatma işlemi tamamlandı.");
        }

        private void MicroCancel(object sender, EventArgs e)
        {
            Change_stGenelId(gvMicro, 3, "İptal işlemi tamamlandı.");
        }

        private void Change_stGenelId(GridView gv, int durumId, string message)
        {
            string kargoIds = gv.GetSelectedValues("KargoId");
            if (kargoIds.Length > 0)
            {
                ExecuteNonQuery("UpdKargoDurum", kargoIds, durumId);
                XtraMessageBox.Show(message);
            }
        }

        void gvMicro_DoubleClick(object sender, EventArgs e)
        {
            KargoDuzenle(gvMicro);
        }

        void gvProforma_DoubleClick(object sender, EventArgs e)
        {
            KargoDuzenle(gvProforma);
        }

        private void KargoDuzenle(GridView gv)
        {
            if (gv.GetFocusedDataRow() != null)
            {
                int kargoId = (int)gv.GetFocusedDataRow()["kargoId"];

                if (kargoId > 0)
                {
                    using (KargoDuzenle kDuzenle = new KargoDuzenle(kargoId))
                        kDuzenle.ShowDialog();
                    Guncelle();
                }
            }
        }

        public static RepositoryItemHyperLinkEdit HyperLink()
        {
            RepositoryItemHyperLinkEdit riFatura = new RepositoryItemHyperLinkEdit();
            riFatura.SingleClick = true;
            return riFatura;
        }

        void gvMicro_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            RowCellClick("MEX-", gvMicro, e);
        }

        void gvProforma_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            RowCellClick("S", gvProforma, e);
        }

        private static void RowCellClick(string tip, GridView gv, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (!(e.Column.FieldName == "KargoF" || e.Column.FieldName == "MalzemeF") || e.RowHandle < 0)
                return;

            int kargoId = gv.GetFocusedDataRow()["KargoId"].ToInt(-1);
            if (e.Column.FieldName == "KargoF")
                if (gv.GetFocusedDataRow()["KargoF"].ToStr() != string.Empty)
                    using (KargoFaturaDetay faturaDetay = new KargoFaturaDetay(kargoId, 1, tip))
                        faturaDetay.ShowDialog();
            //int kargoId = gv.GetFocusedDataRow()["KargoId"].ToInt(-1);
            //using (KargoFaturaDetay faturaDetay = new KargoFaturaDetay(kargoId, faturaTipiId))
            //    faturaDetay.ShowDialog();
            if (e.Column.FieldName == "MalzemeF")
                if (gv.GetFocusedDataRow()["MalzemeF"].ToStr() != string.Empty)
                    using (KargoFaturaDetay faturaDetay = new KargoFaturaDetay(kargoId, 2, tip))
                        faturaDetay.ShowDialog();
        }

        private void chkShowAll_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Guncelle();
        }
    }
}